package br.rc.unesp.tcc.resources;

/**
 * http://dafionsoftware.blogspot.com.br/2011/05/how-to-call-jersey-restful-service-from.html
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


import br.rc.unesp.tcc.model.bean.InvoiceDto;
import br.rc.unesp.tcc.model.bean.InvoiceListDto;

import com.sun.jersey.api.json.JSONWithPadding;

@Path("/myresource")
public class MyResource {

	@GET
	@Produces("text/plain")
	public String getIt() {
		return "Got it!";
	}

	@GET
	@Path("invoicejson/{accountId}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getInvoicesForCustomerJson(
			@PathParam(value = "accountId") String accountId) {

		InvoiceListDto invoices = generateMockData();

		return Response.ok(invoices).build();
	}

	@GET
	@Path("invoicejsonp/{accountId}")
	@Produces({ "application/x-javascript" })
	public JSONWithPadding getInvoicesForCustomerJsonp(
			@PathParam(value = "accountId") String accountId,
			@QueryParam("callback") String callback) {

		InvoiceListDto invoices = generateMockData();

		return new JSONWithPadding(invoices, callback);
	}
	
	private InvoiceListDto generateMockData() {
		List<InvoiceDto> invoices = new ArrayList<InvoiceDto>();
		invoices.add(new InvoiceDto(1, "37897-001", new Date(), 58.92));
		invoices.add(new InvoiceDto(2, "37897-002", new Date(), 293.63));
		invoices.add(new InvoiceDto(3, "37897-003", new Date(), 173.3));
		invoices.add(new InvoiceDto(4, "37897-004", new Date(), 170.71));
		return new InvoiceListDto(invoices);
	}
}
